package work;

import work.fram.JDBCConnect;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

/**
 * 字典值映射生成器
 */
public class DictMapping {

    private static Map<String, String> map = new HashMap<>();

    private static void put(String key, String value) {
        if (map.containsKey(key) && !map.get(key).equals(value)) {
            System.out.println("重复键值:" + key + "=" + value);
        } else {
            map.put(key, value);
        }
    }


    static {

        Statement statement = JDBCConnect.getStatement();

        if (statement == null) {
            throw new RuntimeException("无法获取jdbc连接");
        }


        try {
            ResultSet resultSet = statement.executeQuery("SELECT NAME,CODE FROM sys_dict");

            while (resultSet.next()) {
                String name = resultSet.getString(1);
                String code = resultSet.getString(2);
                if (code != null && name != null) {
                    put(name.trim(), code.trim());
                }
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }


    }


    public static Map<String, String> getMap() {
        return map;
    }

    public static String toCode(String name) {
        return map.getOrDefault(name, name);
    }

    public static void main(String[] args) {
        System.out.println(toCode("大学本科"));
        System.out.println(toCode("大学专科"));
    }

}
